<template>
  <div class="article-detail-container">
    <!-- <NavBar /> -->
    <div class="article-detail-main">
      <el-card v-if="article" class="article-detail-card">
        <div class="article-detail-header">
          <span class="article-detail-title">{{ article.title }}</span>
          <el-button v-if="isAuthor" type="primary" size="small" class="edit-article"
            @click="editArticle">编辑文章</el-button>
        </div>
        <div class="article-detail-meta">
          <span>
            作者：<el-link class="meta-link" type="primary" :underline="false"
              @click="toAuthorDetail(article.author_id)">{{ article.author_username }}</el-link>
          </span>
          <span>
            分类：<el-link class="meta-link" type="success" :underline="false"
              @click="toCategoryDetail(article.category_id)">{{ article.category_name }}</el-link>
          </span>
          <span>标签：
            <el-tag v-for="tag in article.tags" :key="tag" type="info" size="small">{{ tag }}</el-tag>
          </span>
          <span>时间：{{ article.created_time }}</span>
          <span>阅读量：{{ article.views }}</span>
        </div>
        <v-md-editor :model-value="article.body" mode="preview" />
      </el-card>
      <el-empty v-else description="未找到文章" />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
// import NavBar from '@/components/NavBar.vue';
import blogApi from '../api/blog.js';
import { useUserStore } from "../stores/user.js";
import { useTabStore } from "../stores/tab.js";

const tabStore = useTabStore()
const route = useRoute();
const router = useRouter();
const article = ref(null);
const isAuthor = ref(false); // 这里可根据实际登录用户判断
const userStore = useUserStore();

const getArticleDetail = async (id) => {
  const res = await blogApi.GetArticleDetail(id);
  if (res.code === 0) {
    article.value = res.data;
    // 假设后端返回 author 字段，且有当前登录用户信息
    isAuthor.value = res.data.author_username == userStore.state.username;
  }
};

const editArticle = () => {
  if (article.value?.id) {
    router.push({ name: 'EditArticle', params: { id: article.value.id } });
  }
};

const toAuthorDetail = () => {
  console.log(article.value.author_id);
  // router.push({ name: 'AuthorDetail', params: { id: article.value.author_id } });
}

const toCategoryDetail = () => {
  console.log(article.value.category_id);
  // router.push({ name: 'CategoryDetail', params: { id: article.value.category_id } });
}

onMounted(() => {
  const id = parseInt(route.params.id, 10);
  if (id) {
    getArticleDetail(id);
  }
  tabStore.setPageName(route.name)
});
</script>

<style scoped>
.article-detail-container {
  background: var(--main-bg);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.article-detail-main {
  width: 100%;
  max-width: 1200px;
  margin: 32px auto 0 auto;
}

.article-detail-card {
  border-radius: 8px;
  box-shadow: 0 2px 8px var(--shadow);
  padding: 32px 24px;
}

.article-detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.article-detail-title {
  font-size: 26px;
  font-weight: bold;
  color: var(--text-primary);
}

.edit-article {
  background-color: var(--highlight) !important;
  border-color: var(--highlight) !important;
}

.article-detail-meta {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 18px;
  display: flex;
  flex-wrap: wrap;
  gap: 18px;
}

.meta-link {
  margin-right: 12px;
  cursor: pointer;
  font-weight: 500;
  transition: background 0.2s, color 0.2s;
  border-radius: 4px;
  padding: 2px 8px;
}

.meta-link:hover {
  background: var(--highlight, #e6f7ff);
  color: var(--el-color-primary, #409EFF);
}
</style>
